package com.mayikt.days005;

import java.util.concurrent.locks.LockSupport;

/**
 * @ClassName MayiktTask
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
public class MayiktTask<V> implements Runnable {
    private MyCallable<V> myCallable;
    private V result;
    private Thread cuThread;

    private Object lock = new Object();

    public MayiktTask(MyCallable myCallable) {
        this.myCallable = myCallable;
    }

    @Override
    public void run() {
        result = myCallable.call();
        synchronized (lock) {
            lock.notify();
        }
//        LockSupport.unpark(cuThread);
    }

    public V get() {
        if (result != null) {
            return result;
        }
        cuThread = Thread.currentThread();
//        LockSupport.park(cuThread);
        synchronized (lock) {
            try {
                lock.wait();
            } catch (Exception e) {

            }
        }
        return result;
    }
}
